/*
 * @Author: dadadaXU 1413107032@qq.com
 * @Date: 2024-10-16 22:18:53
 * @LastEditors: dadadaXU 1413107032@qq.com
 * @LastEditTime: 2025-01-05 16:13:26
 * @FilePath: /FileBackup/test/test_CArgumentsParser.cpp
 * @Description: 测试命令行参数解析 CArgumentsParser 模块
 */

#include <gtest/gtest.h>
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
#include "../include/argumentsParser.hpp"

class CArgumentsParserTest : public ::testing::Test
{
protected:
	CArgumentsParser *parser;

	void SetUp() override
	{
		parser = new CArgumentsParser();
	}

	void TearDown() override
	{
		if (parser != nullptr)
		{
			delete parser;
			parser = nullptr;
		}
	}
};

/// @brief 数据参数解析测试
TEST_F(CArgumentsParserTest, Parse)
{
	// 模拟命令行参数
	std::vector<std::string> args =
		{"filebackup", "-b", "-i", "../testSample", "-c"};
	int argc = args.size();
	char **argv = new char *[argc];
	for (int i = 0; i < argc; ++i)
		argv[i] = const_cast<char *>(args[i].c_str());

	// 调用函数并验证结果
	EXPECT_TRUE(parser->Parse(argc, argv)) << "参数解析失败" << std::endl;

	// 参数检查
	EXPECT_TRUE(parser->userFlag.backup);
	EXPECT_TRUE(parser->userFlag.input);
	std::cout << "inputPath: " << parser->inputPath << std::endl;
	EXPECT_TRUE(parser->userFlag.compress);

	if (argv != nullptr)
		delete[] argv;
}

/// @brief 数据备份参数解析测试
TEST_F(CArgumentsParserTest, CheckBackupArguments)
{
	// 设置测试环境
	parser->userFlag.backup = true;
	parser->userFlag.restore = false;
	parser->userFlag.viewInfo = false;
	parser->userFlag.metadata = false;

	/* 路径检查 */
	parser->userFlag.input = true;
	parser->inputPath = std::filesystem::path("../testSample");
	parser->userFlag.output = true;
	parser->outputPath = std::filesystem::current_path();

	/* 文件过滤参数 */
	parser->userFlag.filterPath = true;
	parser->fileFilterArguments.path = std::string("^[A-Za-z0-9]+$"); // 英文和数字

	parser->userFlag.filterType = true;
	parser->fileTypes = std::string("-DS"); // TODO 返回FileType类型参数

	parser->userFlag.filterName = true;
	parser->fileFilterArguments.name = std::string("^\\w+$"); // 数字、英文字母或者下划线组成的字符串

	parser->userFlag.filterSize = true;
	parser->fileSize = std::string("0 1000");

	parser->userFlag.atime = true;
	parser->atime = std::string("1970-01-01 00:00:00 2024-12-31 23:59:59");
	parser->userFlag.mtime = true;
	parser->mtime = std::string("1970-01-01 00:00:00 2024-12-31 23:59:59");
	parser->userFlag.ctime = true;
	parser->ctime = std::string("1970-01-01 00:00:00 2024-12-31 23:59:59");

	/* 文件备注信息长度 */
	parser->userFlag.message = true;
	parser->message = std::string("I have a dream!!!");

	// 调用函数并验证结果
	EXPECT_TRUE(parser->CheckBackupArguments()) << "数据备份参数解析失败" << std::endl;
	ASSERT_EQ(parser->fileFilterArguments.mode, 127) << "文件过滤模数 mode " << std::endl;
	ASSERT_EQ(parser->fileFilterArguments.types, (FILE_TYPE_NORMAL | FILE_TYPE_DIRECTORY | FILE_TYPE_SYMBOLIC_LINK));
	ASSERT_EQ(parser->fileFilterArguments.size_from, 0) << "文件大小 from " << std::endl;
	ASSERT_EQ(parser->fileFilterArguments.size_to, 1000) << "文件大小 to " << std::endl;

	std::cout << "atime: " << std::endl
			  << std::ctime(&parser->fileFilterArguments.atime_from)
			  << std::ctime(&parser->fileFilterArguments.atime_to);
	std::cout << "mtime: " << std::endl
			  << std::ctime(&parser->fileFilterArguments.mtime_from)
			  << std::ctime(&parser->fileFilterArguments.mtime_to);
	std::cout << "ctime: " << std::endl
			  << std::ctime(&parser->fileFilterArguments.ctime_from)
			  << std::ctime(&parser->fileFilterArguments.ctime_to);
}

/// @param argv 使用 `--gtest_filter=TestFixtureName.[TestName]` 指定测试模块
int main(int argc, char **argv)
{
	::testing::InitGoogleTest(&argc, argv);

	return RUN_ALL_TESTS();
}